'''
Created on 8 feb 2010

@author: rlindeman
'''
import unittest
import time
import random

import test.evaluation
import rai.logger.logger as logger
from rai.sem.qasystem import *

class TestQASystemFunctionality(unittest.TestCase):
    
    def setUp(self):
        self.basedir = test.evaluation.__path__[0]
        self.org_grammar = "file:" + self.basedir + "\\org.event.fcfg"
        self.qaSystem = QASystem(grammar_location=self.org_grammar)
        file_location = self.basedir + "\\facts.txt" # read sentences in this file
        self.qaSystem.parseFacts(facts_location=file_location)
        # eventfacts should have this number of parsable sentences
        self.assertEquals(len(self.qaSystem.logic_expressions), 4)      
        # add general rule
        self.qaSystem.logic_expressions.extend(self.getGeneralRules())


    def getGeneralRules(self):
        rules = []
        rules.append(self.qaSystem.grammarUtil.parseExpr("man(Bob)"))
        rules.append(self.qaSystem.grammarUtil.parseExpr("man(Steven)"))
        rules.append(self.qaSystem.grammarUtil.parseExpr("man(John)"))
        rules.append(self.qaSystem.grammarUtil.parseExpr("man(Vincent)"))
        rules.append(self.qaSystem.grammarUtil.parseExpr("man(Joe)"))
        rules.append(self.qaSystem.grammarUtil.parseExpr("woman(Suzie)"))
        rules.append(self.qaSystem.grammarUtil.parseExpr("woman(Mary)"))
        rules.append(self.qaSystem.grammarUtil.parseExpr("woman(Kim)"))
        rules.append(self.qaSystem.grammarUtil.parseExpr("dog(Fido)"))
        #rules.append(self.qaSystem.grammarUtil.parseExpr("all x. (boy(x) -> male(x))"))
        rules.append(self.qaSystem.grammarUtil.parseExpr("all x. (man(x) -> person(x))"))
        #rules.append(self.qaSystem.grammarUtil.parseExpr("all x. (girl(x) -> female(x))"))
        rules.append(self.qaSystem.grammarUtil.parseExpr("all x. (woman(x) -> person(x))"))
        #rules.append(self.qaSystem.grammarUtil.parseExpr("all x. (female(x) -> person(x))"))
        #rules.append(self.qaSystem.grammarUtil.parseExpr("all x. (male(x) -> person(x))"))
        return rules
    
    def testQuestion01(self):
        ques = "Suzie loves Bob"
        self.qaSystem.question(ques)
        proofFound = self.qaSystem.answer()
        print ques + "\t" + str(proofFound)
        
    def testQuestion02(self):
        ques = "Suzie loves Steven"
        self.qaSystem.question(ques)
        proofFound = self.qaSystem.answer()
        print ques + "\t" + str(proofFound)
        
    def testQuestion03(self):
        ques = "Suzie loves Kim"
        self.qaSystem.question(ques)
        proofFound = self.qaSystem.answer()
        print ques + "\t" + str(proofFound)

    def testQuestion04(self):
        ques = "Suzie loves Mary"
        self.qaSystem.question(ques)
        proofFound = self.qaSystem.answer()
        print ques + "\t" + str(proofFound)

    def testQuestion05(self):
        ques = "Suzie loves every woman"
        self.qaSystem.question(ques)
        proofFound = self.qaSystem.answer()
        print ques + "\t" + str(proofFound)

    def testQuestion06(self):
        ques = "Suzie loves a dog"
        self.qaSystem.question(ques)
        proofFound = self.qaSystem.answer()
        print ques + "\t" + str(proofFound)
        
    def testQuestion07(self):
        ques = "Somebody loves Bob"
        self.qaSystem.question(ques)
        proofFound = self.qaSystem.answer()
        print ques + "\t" + str(proofFound)

    def testQuestion08(self):
        ques = "A dog barks"
        self.qaSystem.question(ques)
        proofFound = self.qaSystem.answer()
        print ques + "\t" + str(proofFound)

    def testQuestion09(self):
        ques = "Somebody chases a dog"
        self.qaSystem.question(ques)
        proofFound = self.qaSystem.answer()
        print ques + "\t" + str(proofFound)

    def testQuestion10(self):
        ques = "Bob chases a dog"
        self.qaSystem.question(ques)
        proofFound = self.qaSystem.answer()
        print ques + "\t" + str(proofFound)

    def testQuestion11(self):
        ques = "Bob chases a person"
        self.qaSystem.question(ques)
        proofFound = self.qaSystem.answer()
        print ques + "\t" + str(proofFound)


if __name__ == "__main__":
    unittest.main() 